package com.azunitech.apm.bundles.events;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.azunitech.apm.common.api.IEventProxyService;
import com.azunitech.apm.events.Events;

public class Activator implements BundleActivator {
	private static final Logger log = LoggerFactory.getLogger(Activator.class);
	private ServiceRegistration<?> eventProxyServiceRegistration;
	
	public void start(BundleContext context) throws Exception {
		log.debug("start");

		//Register Event Handler
		String[] topics = new String[] {Events.EVENTS_TEST_REPOSTGENERATOR};
        Dictionary<String, Object> props = new Hashtable<String, Object>();
        props.put(EventConstants.EVENT_TOPIC, topics);
        context.registerService(EventHandler.class.getName(), new ReportEventHandler() , props);    	
		
        //Register Event Proxy Service
		IEventProxyService proxyService = new BundleEventProxyServiceImpl(context);
		eventProxyServiceRegistration = context.registerService(IEventProxyService.class.getName(), proxyService, null);		
    }

	public void stop(BundleContext arg0) throws Exception {
		log.debug("stop");
		eventProxyServiceRegistration.unregister();
	}
}